//
//  RTJExperienceGoldController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/11.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJExperienceGoldController.h"
#import "RTJBidRecordsCell.h"
#import "RTJForgetPassWordController.h"

@interface RTJExperienceGoldController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSDictionary *data;

@end

@implementation RTJExperienceGoldController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"体验金";
    self.navigationController.navigationBarHidden = NO;
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    
    [self loadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsCell";
    
    RTJBidRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsCell" owner:self options:nil][1];
    }
    
    NSInteger learn_load = [RTJUserTool getTheUserModel].learn_load;
    
    if (learn_load == 0) {
        
        cell.tiyanTitleLab.text = [NSString stringWithFormat:@"%@",_data[@"learn_type"][@"name"]];
        cell.moneyLab2.text = [NSString stringWithFormat:@"%@元",_data[@"learn_type"][@"money"]];
        cell.useTime.text = [NSString stringWithFormat:@"有效期至：%@",_data[@"learn_type"][@"end_time"]];
        cell.touziName.text = [NSString stringWithFormat:@"%@",_data[@"learn"][@"description"]];
        cell.moneyLab.hidden = YES;
        cell.touzitime.hidden = YES;
        cell.touzishouyi.hidden = YES;
        cell.lab1.hidden = YES;
        NSString *str = @"%";
        cell.rateLab.text = [NSString stringWithFormat:@"%@%@",_data[@"learn"][@"rate"],str];
        cell.touziDays.text =  [NSString stringWithFormat:@"%@",_data[@"learn"][@"time_limit"]];
        cell.lab.text = @"预期年化";
        [cell.useBtn setTitle:@"立即使用" forState:UIControlStateNormal];
        [cell.tiyanBtn setTitle:@"立即体验" forState:UIControlStateNormal];
        [cell.useBtn addTarget:self action:@selector(experienceimmediately) forControlEvents:UIControlEventTouchUpInside];
        [cell.tiyanBtn addTarget:self action:@selector(experienceimmediately) forControlEvents:UIControlEventTouchUpInside];

        
    }else{
        cell.moneyLab2.hidden = YES;
        cell.moneyLab.hidden = NO;
        cell.touzitime.hidden = NO;
        cell.touzishouyi.hidden = NO;
        cell.lab1.hidden = NO;
        
        cell.tiyanTitleLab.text = [NSString stringWithFormat:@"%@",_data[@"learn_type"][@"name"]];

        cell.moneyLab.text = [NSString stringWithFormat:@"%@元",_data[@"learn_type"][@"money"]];
        cell.useTime.text = [NSString stringWithFormat:@"有效期至：%@",_data[@"learn_type"][@"end_time"]];
        
        cell.touzitime.text = [NSString stringWithFormat:@"出借期限:%@天",_data[@"learn"][@"time_limit"]];
        cell.touzishouyi.text = [NSString stringWithFormat:@"预期收益:%@元",_data[@"learn"][@"profit"]];
        
        cell.touziName.text = [NSString stringWithFormat:@"%@",_data[@"learn"][@"description"]];
        cell.rateLab.text = [NSString stringWithFormat:@"%@元",_data[@"learn"][@"profit"]];
        cell.touziDays.text =  [NSString stringWithFormat:@"%@",_data[@"learn"][@"time_limit"]];
        cell.lab1.text = [NSString stringWithFormat:@"%@后可领取",_data[@"learn_load"][@"end_date"]];
        cell.lab.text = @"预期收益";
        [cell.useBtn setTitle:@"已使用" forState:UIControlStateNormal];
        NSString *is_send = [NSString stringWithFormat:@"%@",_data[@"learn_load"][@"is_send"]];
        [cell.tiyanBtn setTitle:[is_send isEqualToString:@"0"] ? @"领取收益":@"已领取收益" forState:UIControlStateNormal];
        cell.tiyanBtn.userInteractionEnabled = [is_send isEqualToString:@"0"] ? YES : NO;
        [cell.tiyanBtn addTarget:self action:@selector(experienceimmediately) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}

-(void)loadData{
    
    
    [SVProgressHUD  showWithStatus:nil];
    [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
    
    [RTJNetWork asyncRequestWithURL:@"user/learn" params:nil method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];

        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                self.data = data[@"data"];
                [self.tableView reloadData];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                [self.view showEmptyViewWithStr:data[@"msg"] image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
            
        }
        
        
    }];
}


-(void)experienceimmediately{
    
    NSInteger learn_load = [RTJUserTool getTheUserModel].learn_load;
    
    if (learn_load == 1) {//
        
        [SVProgressHUD  showWithStatus:nil];
        
        NSString *IdStr = [NSString stringWithFormat:@"%@",_data[@"learn_load"][@"id"]];
        NSDictionary *dic = @{@"learn_load_id":IdStr};
        [RTJNetWork asyncRequestWithURL:@"user/get_learn_interest" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
            
            [SVProgressHUD dismiss];
            
            
            if (success) {
                
                if ([data[@"status"]integerValue] == 1) {
                    
                    [SVProgressHUD showSuccessWithStatus:@"收益领取成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                    
                }else{
                    
                    [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

            }
            
            
        }];

        
        
    }else{
        
        SpecialAlertView *special = [[SpecialAlertView alloc]initWithLevelType:YES];
        
        [special withSureClick:^(NSInteger tags,NSString *password,UIView *alertView) {
            
            [UIView animateWithDuration:0.3 animations:^{
                [alertView removeFromSuperview];
            }];
            
            if (tags == 110) {
                
//                [UIView animateWithDuration:0.3 animations:^{
//                    [alertView removeFromSuperview];
//                }];
                
                RTJForgetPassWordController *vc = [[RTJForgetPassWordController alloc]init];
                vc.isPayPassWord = YES;
                vc.title = @"找回支付密码";
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
                
                if (password.length  == 0) {
                    
                    [SVProgressHUD showErrorWithStatus:@"请输入交易密码"];
                    return ;
                }
                //验证支付密码是否正确
                [RTJNetWork asyncRequestWithURL:@"user/check_pay_password" params:@{@"pay_password":password} method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
                    
                    if (success) {
                        
                        if ([data[@"status"]integerValue]==1) {
                            
//                            [UIView animateWithDuration:0.3 animations:^{
//                                [alertView removeFromSuperview];
//                            }];
                            
                            //开始使用
                            [self begintouse:password];
                            
                        }else{
                            
                            
                            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",data[@"msg"]]];
                        }
                        
                    }else{
                        
                        [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"密码错误:%@",errorMsg]];

                    }
                }];
                
            }
            
        }];

        
        
    }
    
}

-(void)begintouse:(NSString *)passWord{
    
    [SVProgressHUD  showWithStatus:nil];
    
    NSString *IdStr = [NSString stringWithFormat:@"%@",_data[@"learn"][@"id"]];
    NSString *lsl_id = [NSString stringWithFormat:@"%@",_data[@"learn_type"][@"lis_id"]];
    NSDictionary *dic = @{@"id":IdStr,
                          @"paypassword":passWord,
                          @"lsl_id":lsl_id
                          };
    [RTJNetWork asyncRequestWithURL:@"user/use_learn" params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [SVProgressHUD showSuccessWithStatus:@"体验金使用成功"];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
      
        
    }];
    

}

@end
